package com.pirestupppai.widgets.view;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pirestupppai.R;
import com.pirestupppai.adapter.ViewPagerAdapter;
import com.pirestupppai.bean.HomeBanner;
import com.pirestupppai.common.Constant;
import com.pirestupppai.ui.activity.WebActivity;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装的banner
 *
 * author: xiaoluo
 * date: 2017/6/29 10:45
 */
public class BannerView extends RelativeLayout {
    private Context mContext;
    private AutoScrollViewPager mViewpager;
    private LinearLayout mIndicatorLayout;

    private List<ImageView> mIndicatorList = new ArrayList<>();
    private List<View> mViewList = new ArrayList<>();
    private List<HomeBanner> mBannerList = new ArrayList<>();

    public BannerView(Context context) {
        this(context, null);
    }

    public BannerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;

        LayoutInflater.from(context).inflate(R.layout.widget_banner_view, this, true);
        mViewpager = (AutoScrollViewPager) findViewById(R.id.banner_view_viewpager);
        mIndicatorLayout = (LinearLayout) findViewById(R.id.banner_view_indicator);
        initListeners();
    }

    private void initListeners() {
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0, length = mBannerList.size(); i < length; i++) {
                    if (position == i) {
                        mIndicatorList.get(i).setBackgroundResource(R.drawable.shape_scroll_select);
                    } else {
                        mIndicatorList.get(i).setBackgroundResource(R.drawable.shape_scroll_unselect);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置数据
     */
    public void setBannerData(List<HomeBanner> list) {
        mBannerList = list;
        if (mBannerList != null && mBannerList.size() > 0) {
            setVisibility(VISIBLE);
        } else {
            setVisibility(GONE);
            return;
        }

        initIndicators();
        initViews();
    }

    /**
     * 初始化指示点
     */
    private void initIndicators() {
        mIndicatorLayout.removeAllViews();
        mIndicatorList.clear();

        LinearLayout.LayoutParams scrollLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        for (int i = 0, length = mBannerList.size(); i < length; i++) {
            ImageView scrollImage = new ImageView(mContext);
            scrollImage.setScaleType(ImageView.ScaleType.FIT_XY);
            if (i == 0) {
                scrollImage.setBackgroundResource(R.drawable.shape_scroll_select);
                scrollLayoutParams.setMargins(0, 0, 0, 0);
            } else {
                scrollImage.setBackgroundResource(R.drawable.shape_scroll_unselect);
                scrollLayoutParams.setMargins(Utils.dpToPx(mContext, 5), 0, 0, 0);
            }
            scrollImage.setLayoutParams(scrollLayoutParams);

            mIndicatorLayout.addView(scrollImage);
            mIndicatorList.add(scrollImage);
        }
    }

    /**
     * 初始化视图
     */
    private void initViews() {
        mViewList.clear();
        RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                Utils.dpToPx(mContext, mContext.getResources().getDimension(R.dimen.banner_height)));

        ImageLoadUtil imageLoader = ImageLoadUtil.getInstance();
        for (int i = 0, length = mBannerList.size(); i < length; i++) {
            final HomeBanner homeBanner = mBannerList.get(i);
            ImageView bannerImage = new ImageView(mContext);
            bannerImage.setLayoutParams(imageParams);
            bannerImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageLoader.loadImage(homeBanner.getBanner_image_url(), bannerImage, ImageLoadUtil.LoadOption.LOAD_BANNER);
            mViewList.add(bannerImage);

            bannerImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg) {
                    Intent intent = new Intent(mContext, WebActivity.class);
                    intent.putExtra(Constant.IntentKey.WEBVIEW_URL, homeBanner.getBanner_image_url());
                    mContext.startActivity(intent);
                }
            });
        }

        mViewpager.setAdapter(new ViewPagerAdapter(mViewList));
        mViewpager.setInterval(3000);  // 设置自动滚动的间隔时间，单位为毫秒
        mViewpager.startAutoScroll();  // 启动自动滚动
    }
}
